package ar.uade.edu.fain.ad.client.web.servlet.salon.cierreMesa;

import ar.uade.edu.fain.ad.client.web.servlet.salon.SalonServlet;
import ar.uade.edu.fain.ad.common.exception.PedidoCerradoException;
import ar.uade.edu.fain.ad.common.transfer.PedidoVO;
import ar.uade.edu.fain.ad.common.transfer.SalonVO;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CierreMesaServlet
 */
public class CierreMesaServlet extends SalonServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see SalonServlet#SalonServlet()
	 */
	public CierreMesaServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		final Long salonId = 
				(Long) request.getSession().getAttribute("salonId");
		final SalonVO salonVO = new SalonVO();
		salonVO.setId(salonId);
		final Set<PedidoVO> pedidos = 
				getSalonServiceDelegate().buscarPedidosActivos(salonVO);
		request.setAttribute("pedidos", pedidos);
		forward(getViewBasePath() + "cierreMesa/cerrarMesa.jsp", 
				request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		final Long pedidoId = Long.valueOf(request.getParameter("pedidoId"));
		final PedidoVO pedidoVO = new PedidoVO();
		pedidoVO.setId(pedidoId);
		try {
			getSalonServiceDelegate().cerrarPedido(pedidoVO);
			request.setAttribute("pedido", pedidoVO);
			forward(getViewBasePath() + "cierreMesa/mesaCerrada.jsp", 
					request, response);
		} catch (PedidoCerradoException e) {
			request.setAttribute("pedido", e.getPedido());
			forward(getViewBasePath() + "cierreMesa/pedidoYaCerrado.jsp", 
					request, response);
		}
	}

}
